import {
  getContentTable,
  editContentRecord
} from '../../api/content'
import {
  GRAPHIC_LIST,
  GRAPHIC_CURRENT_ROW,
  GRAPHIC_DATA_COUNT,
  GRAPHIC_HISTORY_DATA,
  GRAPHIC_PAGE_SIZE,
  TW_HISTORY_DATA
} from '../motations-type'
import qqgroup from '@/assets/images/qq-group2.jpg'

export default {
  state: {
    graphicList: [],
    graphicDataCount: null,
    graphicPageSize: 20,
    imgPageSize: 8,
    twHistoryData: [],
    graphicHistoryData: [],
    row: {
      img: '',
      host: '',
      ctime: '',
      name: '',
      subtitle: '',
      uptime: '',
      type: '',
      content: '',
      parseURL: ''
    }
  },
  getters: {
    graphicList: state => state.graphicList
  },
  mutations: {
    [GRAPHIC_LIST] (state, data) {
      state.graphicList = data
    },
    [GRAPHIC_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [GRAPHIC_DATA_COUNT] (state, data) {
      state.graphicDataCount = data
    },
    [GRAPHIC_HISTORY_DATA] (state, data) {
      state.graphicHistoryData = data
    },
    [GRAPHIC_PAGE_SIZE] (state, data) {
      state.graphicPageSize = data
    },
    [TW_HISTORY_DATA] (state, data) {
      state.twHistoryData = data
    }
  },
  actions: {
    async getGraphicList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getContentTable(data)
      let host = resp.data.host
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          img: item.img,
          host: host,
          ctime: item.ctime,
          name: item.name,
          subtitle: item.subtitle,
          uptime: item.uptime,
          type: item.type,
          content: item.content,
          parseURL: item.parseURL
        }]
      })
      commit(GRAPHIC_LIST, newData)
      commit(GRAPHIC_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.graphic.graphicPageSize) {
        commit(GRAPHIC_HISTORY_DATA, newData)
      } else {
        commit(GRAPHIC_HISTORY_DATA, newData.slice(0, rootState.graphic.graphicPageSize))
      }
      if (newData.length < rootState.graphic.imgPageSize) {
        commit(TW_HISTORY_DATA, newData)
      } else {
        commit(TW_HISTORY_DATA, newData.slice(0, rootState.graphic.imgPageSize))
      }
    },
    async editGraphicListData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editContentRecord(data)
      return resp
    },
    async getGraphicListCurrentRow ({ commit, rootState }, row) {
      commit(GRAPHIC_CURRENT_ROW, row)
    },
    async changeGraphicListPage ({ commit, rootState }, index) {
      let data = rootState.graphic.graphicList
      let _start = (index - 1) * rootState.graphic.graphicPageSize
      let _end = index * rootState.graphic.graphicPageSize
      commit(GRAPHIC_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeGraphicListPageSize ({ commit, rootState }, value) {
      commit(GRAPHIC_PAGE_SIZE, value)
    },
    async changeTwPage ({ commit, rootState }, index) {
      let data = rootState.graphic.graphicList
      let _start = (index - 1) * rootState.graphic.imgPageSize
      let _end = index * rootState.graphic.imgPageSize
      commit(TW_HISTORY_DATA, data.slice(_start, _end))
    }
  }
}
